//
//  ActivityViewController.m
//  DouBan
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityTableViewCell.h"
#import "ActivityModel.h"
#import "UIColor+RandomColor.h"
#import "ActivityDetailViewController.h"
@interface ActivityViewController ()<UITableViewDelegate, UITableViewDataSource>
/**  活动列表 */
@property (nonatomic, retain) UITableView *activityTableView;

/**  数据源数组 */
@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation ActivityViewController

- (void)dealloc
{
    [_activityTableView release];
    [_dataSource release];
    [super dealloc];
}

static NSString *const activityTableViewCell = @"activityTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //先加载数据
    [self dataHandle];
    
    //加载列表
    [self createActivityTableView];
}

//数据解析
- (void)dataHandle
{
    self.dataSource = [NSMutableArray array];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSArray *dataArray = [dictionary objectForKey:@"events"];
    for (NSDictionary *dic in dataArray) {
        ActivityModel *activityModel = [[ActivityModel alloc] init];
        [activityModel setValuesForKeysWithDictionary:dic];
        [self.dataSource addObject:activityModel];
        [activityModel release];
    
    }
    
    [self.activityTableView reloadData];
    
}


//初始化列表
- (void)createActivityTableView
{
    self.activityTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44) style:UITableViewStylePlain];
    [self.activityTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_activityTableView];
    [_activityTableView release];
    
    self.activityTableView.delegate = self;
    self.activityTableView.dataSource = self;

    [self.activityTableView registerNib:[UINib nibWithNibName:@"ActivityTableViewCell" bundle:nil] forCellReuseIdentifier:activityTableViewCell];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityModel *atModel = [self.dataSource objectAtIndex:indexPath.row];
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:activityTableViewCell];
    cell.model = atModel;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityDetailViewController *advc = [[ActivityDetailViewController alloc] init];
    [self.navigationController pushViewController:advc animated:YES];
    [advc release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
